package com.hongshu.file.service.impl;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.common.utils.IoUtils;
import com.hongshu.common.core.domain.R;
import com.hongshu.common.core.enums.OssProviderEnum;
import com.hongshu.file.domain.OssConfigDTO;
import com.hongshu.file.service.ISysFileService;
import com.hongshu.file.utils.FileUploadUtils;
import com.hongshu.system.api.RemoteWebService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.util.Map;

/**
 * Minio 文件存储
 *
 * @author hongshu
 */
@Service
public class MinioSysFileServiceImpl implements ISysFileService {

    @Autowired
    private RemoteWebService remoteWebService;


    /**
     * Minio文件上传接口
     *
     * @param file 上传的文件
     * @return 访问地址
     */
    @Override
    public String uploadFile(MultipartFile file) {
        R<?> r = remoteWebService.getOssConfig();
        if (r == null || r.getData() == null) {
            throw new IllegalStateException("未获取到 OSS 配置：remoteWebService.getOssConfig() 返回为空");
        }
        Map<String, OssConfigDTO> ossMap = JSONUtil.toBean(
                JSONUtil.parseObj(r.getData()),
                new TypeReference<Map<String, OssConfigDTO>>() {
                }, false);
        OssConfigDTO ossConfig = ossMap.get(OssProviderEnum.MINIO.getCode());
        if (ossConfig == null) {
            throw new IllegalStateException("未找到Minio OSS 配置，key=" + OssProviderEnum.MINIO.getCode());
        }
        // endpoint 兼容：优先 domain（MinIO 场景），其次 endpoint
        String endpoint = StrUtil.blankToDefault(ossConfig.getDomain(), ossConfig.getEndpoint());
        if (StrUtil.hasBlank(ossConfig.getDomain(), ossConfig.getAccessKey(), ossConfig.getSecretKey(), ossConfig.getBucketName())) {
            throw new IllegalStateException("Minio OSS 配置不完整，请检查 url/endpoint、accessKey、secretKey、bucketName");
        }

        // 构建临时 MinioClient
        MinioClient client = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(ossConfig.getAccessKey(), ossConfig.getSecretKey())
                .build();

        InputStream inputStream = null;
        try {
            String fileName = FileUploadUtils.extractFilename(file);
            inputStream = file.getInputStream();
            PutObjectArgs args = PutObjectArgs.builder()
                    .bucket(ossConfig.getBucketName())
                    .object(fileName)
                    .stream(inputStream, file.getSize(), -1)
                    .contentType(StrUtil.blankToDefault(file.getContentType(), "application/octet-stream"))
                    .build();
            client.putObject(args);

            // 拼接访问地址：优先 domain，其次 endpoint/url
            String base = StrUtil.emptyToDefault(ossConfig.getDomain(), endpoint);
            base = base.replaceAll("/+$", "");
            if (!base.startsWith("http://") && !base.startsWith("https://")) {
                base = "http://" + base;
            }
            return base + "/" + ossConfig.getBucketName() + "/" + fileName;
        } catch (Exception e) {
            throw new RuntimeException("Minio Failed to upload file", e);
        } finally {
            IoUtils.closeQuietly(inputStream);
        }
    }

    @Override
    public String uploadFile(File file) {
        return null;
    }
}
